#include "nr_micro_shell.h"


void shell_ls_cmd(char argc, char *argv)
{
	unsigned int i = 0;
	if (argc > 1)
	{
		if (!strcmp("cmd", &argv[argv[1]]))
		{

			for (i = 0; nr_shell.static_cmd[i].fp != NULL; i++)
			{
				shell_printf("%s",nr_shell.static_cmd[i].cmd);
				shell_printf("\r\n");
			}
		}
		else if (!strcmp("-v", &argv[argv[1]]))
		{
			shell_printf("ls version 1.0.\r\n");
		}
		else if (!strcmp("-h", &argv[argv[1]]))
		{
			shell_printf("useage: ls [options]\r\n");
			shell_printf("options: \r\n");
			shell_printf("\t -h \t: show help\r\n");
			shell_printf("\t -v \t: show version\r\n");
			shell_printf("\t cmd \t: show all commands\r\n");
		}
	}
	else
	{
		shell_printf("ls need more arguments!\r\n");
	}
}


void shell_test_cmd(char argc, char *argv)
{
	unsigned int i;
	shell_printf("test command:\r\n");
	for (i = 0; i < argc; i++)
	{
		shell_printf("paras %d: %s\r\n", i, &(argv[argv[i]]));
	}
}


void shell_led_cmd(char argc, char *argv)
{
    char *param = &(argv[argv[1]]);

    if(strcmp(param, "ON") == 0)
    {
        Led_Shell_Handler(1);
    }
    else if(strcmp(param, "OFF") == 0)
    {
        Led_Shell_Handler(0);
    }
	else
	{
		printf("Error Command! Usage:LED 1 or LED 0\r\n");
	}
}



void shell_runig_time_cmd(char argc, char *argv)
{
	if ( strcmp("time", &argv[argv[1]]) == 0 )
	{
		printf("程序运行总时间为:%d 秒 \r\n", g_iRunTimer/1000);
	}
	else
	{
		shell_printf("command error! \r\n");
	}

}

void shell_ADC_Test_cmd(char argc, char *argv)
{
	char *param = &(argv[argv[1]]);

    if(strcmp(param, "1") == 0)
    {
        ADC_Test_Enable = 1;
    }
    else if(strcmp(param, "0") == 0)
    {
        ADC_Test_Enable = 0;
    }
	else
	{
		printf("Error Command! Usage:adctest 1 or ADCTest 0\r\n");
	}
} 



void shell_PWM_Test_cmd(char argc, char *argv)
{
	uint8_t ch;
	uint8_t duty;
	char *param;
	
	if(argc<3)
	{
		printf("错误指令,使用方法:PWM ch duty (ch:1 or 2)(duty:0~100)\r\n");
		return;
	}
	
	param = &(argv[argv[1]]);
	ch = atoi(param);
	
	if(ch<1 || ch > 2)
	{
		printf("错误指令,使用方法:PWM ch duty (ch:1 or 2)(duty:0~100)\r\n");
		return;
	}
	
	param = &(argv[argv[2]]);
	duty = atoi(param);   // 将数字字符串转换成实际的数字，标准C语言库函数
	// printf("收到数据%d\n", duty);
	if(duty > 100)
	{
		printf("错误指令,使用方法:pwm ch duty (ch:1 or 2)(duty:0~100)\r\n");
		return;
	}
	
	if(ch == 1)
	{
		// TIM_SetCompare1(TIM3, duty%101); // 这才是0到100的占空比的调整
		Set_PWM01_Compare(duty%101);

		printf("Timer3的PWM(PC6)通道1占空比设置为%d\r\n", duty%101);
	}
	else if(ch == 2)
	{
		// TIM_SetCompare2(TIM3, duty%101);
		Set_PWM02_Compare(duty%101);
		printf("Timer3的PWM(PC7)通道2占空比设置为%d\r\n", duty%101);
	}	

} 

void shell_beep_cmd(char argc, char *argv)
{
    char *param = &(argv[argv[1]]);

    if(strcmp(param, "1") == 0)
    {
		// BEEP_ON();
		beep_enable = 1;
    }
    else if(strcmp(param, "0") == 0)
    {
		// BEEP_OFF();
		beep_enable = 0;
    }
	else
	{
		printf("Error Command! Usage:BEEP 1 or BEEP 0\r\n");
	}
}

#if ((FATFS_ENABLE) && (W25QXX_SFUD_ENABLE) &&(USB_MASS_STORAGE_ENABLE))

void shell_file_cmd(char argc, char *argv)
{
	if (argc != 3)
	{
		printf("使用方法: makefile [FileName] [FileData] \r\n");
	}else if (argc == 3)
	{
		char *FileName = &(argv[argv[1]]);
		char *Data = &(argv[argv[2]]);
		Write_to_file(FileName, Data);
		printf("尝试写入文件成功 \r\n");
	}
}
#endif


#ifdef NR_SHELL_USING_EXPORT_CMD
NR_SHELL_CMD_EXPORT(ls, shell_ls_cmd);
NR_SHELL_CMD_EXPORT(test, shell_test_cmd);
#else
const static_cmd_st static_cmd[] =
	{

		// {"AT24C02", },
		{"runing", shell_runig_time_cmd},
		{"ls",   shell_ls_cmd   },
		
		#if ((FATFS_ENABLE) && (W25QXX_SFUD_ENABLE) &&(USB_MASS_STORAGE_ENABLE))
		{"makefile", shell_file_cmd},
		#endif

#if LED_ENABLE
        {"LED",  shell_led_cmd  },
#endif
		
#if BEEP_ENABLE
		{"BEEP", shell_beep_cmd  },
#endif
		
#if INFLASH_ENABLE
		{"flashw",shell_Inflash_Write_cmd},
		{"flashr",shell_Inflash_Read_cmd},
#endif
		
#if ADC_ENABLE
		{"ADCtest",shell_ADC_Test_cmd},
#endif
	
#if PWM_ENABLE		
		{"PWM",shell_PWM_Test_cmd},
#endif
#if FATFS_ENABLE && W25QXX_SFUD_ENABLE
		{"saveSenDat",shell_Save_SensorData_To_SPIFlash_cmd},
		{"readSenDat",shell_Read_SensorData_From_SPIFlash_cmd},
		{"delSenDat",shell_Del_SensorData_SPIFlash_cmd},
#endif
		{"test", shell_test_cmd },
		{"\0",   NULL           }
    };
#endif


